package com.sundergames.videomaton2;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sundergames.videomaton2.parameters.UploadVideoParameters;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private final String ROOT_PATH = "/home/sunder/videomaton";
	private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
		//return "webm";
	}
	
	@RequestMapping(value = "/webm", method = RequestMethod.GET)
	public String webm(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "webm";
	}
	
	@ResponseBody
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public String send(UploadVideoParameters params, MultipartHttpServletRequest request) {
		logger.info("Uploading video");
		
		CommonsMultipartFile video = params.getVideo();
		CommonsMultipartFile audio = params.getAudio();
		
		String filename = DATE_FORMAT.format(new Date());
		
		try {
			File fileVideo = new File(ROOT_PATH + "/video/" + filename + ".webm");
			File fileAudio = new File(ROOT_PATH + "/audio/" + filename + ".wav");
			video.transferTo(fileVideo);
			audio.transferTo(fileAudio);
			logger.info("File saved at:" + fileVideo.getAbsolutePath());
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		return "ok";
	}
	
	@RequestMapping(value = "/sample1", method = RequestMethod.GET)
	public String sample1(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		//return "home";
		return "home/sample/sample1";
	}
	
	@RequestMapping(value = "/sample2", method = RequestMethod.GET)
	public String sample2(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		//return "home";
		return "home/sample/sample2";
	}
}
